package com.family.instagram;

import org.json.JSONObject;

import com.wu.family.MainGroupActivity;
import com.wu.family.R;
import com.wu.family.config.Constant;
import com.wu.family.config.UserInfo;
import com.wu.family.login.BindingWeiboActivity;
import com.wu.family.login.LoginActivity;
import com.wu.family.utils.ToastUtil;
import com.wu.family.views.LoadingDialog;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Toast;

public class InstagramLoginActivity extends Activity {
	
	private WebView 	mWebView;
	private View		mBack;
	private InstagramCallBack callBack;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_instagramlogin);
		mWebView 	= (WebView) findViewById(R.id.instagram_webview);
		mBack 		= findViewById(R.id.instagram_back);
		
		mBack.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				InstagramLoginActivity.this.finish();
			}
		});
		callBack = new InstagramCallBack() {
			
			@Override
			public void onReturn(String result) {
				// TODO Auto-generated method stub
				// result：返回Family接口第三方登录的数据
				System.out.println("onreturn");
				SharedPreferences share = getSharedPreferences(Constant.SHARED_NAME, Context.MODE_PRIVATE);
				Editor editor = share.edit();
				try {
					JSONObject object = new JSONObject(result);
					if(object.getInt("error") == 0){
						JSONObject data = object.getJSONObject("data");
						if (data.getInt("return") == 1) {
							//Editor editor = share.edit();
							/*editor.putString(Constant.UserKey.ACCOUNT, account);
							editor.putString(Constant.UserKey.PASSWORD, password);*/
							editor.putBoolean(Constant.UserKey.REM_PASSWORD, true);
							editor.commit();
							
							Toast.makeText(InstagramLoginActivity.this, "登录成功！", Toast.LENGTH_SHORT).show();
							String m_auth = data.getString("m_auth");
							String p_token = InstagramEngine.getInstance().getToken();
							UserInfo userInfo = UserInfo.getInstance(InstagramLoginActivity.this);
							userInfo.setUid(data.getString("uid"));
							userInfo.setUserName(data.getString("username"));
							userInfo.setName(data.getString("name"));
							userInfo.setAvatarPath(data.getString("avatar"));
							userInfo.setVipstatus(data.getString("vipstatus"));
							userInfo.setIsSinaBind(data.getString("is_sina_bind"));
							userInfo.setIsQQbind(data.getString("is_qq_bind"));
							userInfo.setIsInstagramBind(data.getString("is_instagram_bind"));
							System.out.println("初始化保存的token:" + InstagramEngine.getInstance().getToken());
							if( p_token != null || !p_token.equals("")){
								userInfo.setInstagramToken(p_token);
							}
							userInfo.setM_auth(m_auth);
							if (data.getString("credit")!=null && !data.getString("credit").equals("null") 
									&& !data.getString("credit").equals("0")) {
								ToastUtil.show(InstagramLoginActivity.this, "增加"+data.getString("credit")+"金币值;"
										   +"增加"+data.getString("experience")+"经验值！");
							}
							dialog.dismiss();
							Intent intent = new Intent(InstagramLoginActivity.this, MainGroupActivity.class);
							startActivity(intent);
							finish();
						}else if (data.getInt("return") == -2){
							ToastUtil.show(InstagramLoginActivity.this,  object.getString("msg"));
							finish();
						} else {
							ToastUtil.show(InstagramLoginActivity.this,  object.getString("msg"));
							finish();
						}
					}else {
						ToastUtil.show(InstagramLoginActivity.this,  object.getString("msg"));
							finish();
					}
				} catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
				}
				// 处理一下登录信息的保存
				
				// 珍藏界面新增相册 Instagram
				
				// 关闭当前认证界面
				InstagramLoginActivity.this.finish();
			}
			
			@Override
			public boolean isLogin() {
				// TODO Auto-generated method stub
				// 判断是否已经登录过（onReturn的处理的保存）
				return false;
			}
			
			@Override
			public void clearCache() {
				// TODO Auto-generated method stub
				// 退出Instagram的账户时的处理（浏览器的缓存已经删除，删除onReturn的保存即可）
			}

			@Override
			public void dismiss() {
				if (dialog != null) {
					dialog.dismiss();
				}
			}

			@Override
			public void show() {
				if (dialog != null) {
					//dialog.show();
				}
			}
		};
		InstagramEngine.getInstance().loginInstagram(this, mWebView, callBack);
		dialog = new LoadingDialog.Builder(this).create();
		dialog.setCanceledOnTouchOutside(false);
		dialog.show();
	}
	
	LoadingDialog dialog;
}
